[[...path]].page.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import React from 'react';
  2. import { IUserHasId } from '@growi/core';
  3. import {
  4. NextPage, GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { useTranslation } from 'next-i18next';
  7. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  8. import dynamic from 'next/dynamic';
  9. import Head from 'next/head';
  10. import CountBadge from '~/components/Common/CountBadge';
  11. import PageListIcon from '~/components/Icons/PageListIcon';
  12. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  13. import GrowiContextualSubNavigation from '~/components/Navbar/GrowiContextualSubNavigation';
  14. import { Page } from '~/components/Page';
  15. import styles from '~/components/Page/DisplaySwitcher.module.scss'; // for PageList toc style
  16. import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
  17. import TableOfContents from '~/components/TableOfContents';
  18. import { SupportedAction, SupportedActionType } from '~/interfaces/activity';
  19. import { CrowiRequest } from '~/interfaces/crowi-request';
  20. import { RendererConfig } from '~/interfaces/services/renderer';
  21. import { IShareLinkHasId } from '~/interfaces/share-link';
  22. import {
  23. useCurrentUser, useCurrentPathname, useCurrentPageId, useRendererConfig, useIsSearchPage,
  24. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useDrawioUri, useIsContainerFluid,
  25. } from '~/stores/context';
  26. import { useDescendantsPageListModal } from '~/stores/modal';
  27. import loggerFactory from '~/utils/logger';
  28. import {
  29. CommonProps, getServerSideCommonProps, generateCustomTitle, getNextI18NextConfig,
  30. } from '../utils/commons';
  31. const logger = loggerFactory('growi:next-page:share');
  32. const ShareLinkAlert = dynamic(() => import('~/components/Page/ShareLinkAlert'), { ssr: false });
  33. const ForbiddenPage = dynamic(() => import('~/components/ForbiddenPage'), { ssr: false });
  34. type Props = CommonProps & {
  35. shareLink?: IShareLinkHasId,
  36. isExpired: boolean,
  37. disableLinkSharing: boolean,
  38. isSearchServiceConfigured: boolean,
  39. isSearchServiceReachable: boolean,
  40. isSearchScopeChildrenAsDefault: boolean,
  41. drawioUri: string | null,
  42. rendererConfig: RendererConfig,
  43. };
  44. const SharedPage: NextPage<Props> = (props: Props) => {
  45. useIsSearchPage(false);
  46. useShareLinkId(props.shareLink?._id);
  47. useCurrentPageId(props.shareLink?.relatedPage._id);
  48. useCurrentUser(props.currentUser);
  49. useCurrentPathname(props.currentPathname);
  50. useRendererConfig(props.rendererConfig);
  51. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  52. useIsSearchServiceReachable(props.isSearchServiceReachable);
  53. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  54. useDrawioUri(props.drawioUri);
  55. useIsContainerFluid(props.isContainerFluid);
  56. const { open: openDescendantPageListModal } = useDescendantsPageListModal();
  57. const { t } = useTranslation();
  58. const isNotFound = props.shareLink == null || props.shareLink.relatedPage == null || props.shareLink.relatedPage.isEmpty;
  59. const isShowSharedPage = !props.disableLinkSharing && !isNotFound && !props.isExpired;
  60. const shareLink = props.shareLink;
  61. const title = generateCustomTitle(props, 'GROWI');
  62. return (
  63. <>
  64. <Head>
  65. <title>{title}</title>
  66. </Head>
  67. <DrawioViewerScript />
  68. <ShareLinkLayout>
  69. <div className="h-100 d-flex flex-column justify-content-between">
  70. <header className="py-0 position-relative">
  71. {isShowSharedPage && <GrowiContextualSubNavigation isLinkSharingDisabled={props.disableLinkSharing} />}
  72. </header>
  73. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  74. <div className="flex-grow-1">
  75. <div id="content-main" className="content-main">
  76. <div className="grw-container-convertible">
  77. { props.disableLinkSharing && (
  78. <div className="mt-4">
  79. <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />
  80. </div>
  81. )}
  82. { (isNotFound && !props.disableLinkSharing) && (
  83. <div className="container-lg">
  84. <h2 className="text-muted mt-4">
  85. <i className="icon-ban" aria-hidden="true" />
  86. <span> Page is not found</span>
  87. </h2>
  88. </div>
  89. )}
  90. { (props.isExpired && !props.disableLinkSharing && shareLink != null) && (
  91. <div className="container-lg">
  92. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  93. <h2 className="text-muted mt-4">
  94. <i className="icon-ban" aria-hidden="true" />
  95. <span> Page is expired</span>
  96. </h2>
  97. </div>
  98. )}
  99. {(isShowSharedPage && shareLink != null) && (
  100. <>
  101. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  102. <div className="d-flex flex-column flex-lg-row-reverse">
  103. <div className="grw-side-contents-container">
  104. <div className="grw-side-contents-sticky-container">
  105. {/* Page list */}
  106. <div className={`grw-page-accessories-control ${styles['grw-page-accessories-control']}`}>
  107. { shareLink.relatedPage.path != null && (
  108. <button
  109. type="button"
  110. className="btn btn-block btn-outline-secondary grw-btn-page-accessories
  111. rounded-pill d-flex justify-content-between align-items-center"
  112. onClick={() => openDescendantPageListModal(shareLink.relatedPage.path)}
  113. data-testid="pageListButton"
  114. >
  115. <div className="grw-page-accessories-control-icon">
  116. <PageListIcon />
  117. </div>
  118. {t('page_list')}
  119. <CountBadge count={shareLink.relatedPage.descendantCount} offset={1} />
  120. </button>
  121. ) }
  122. </div>
  123. <div className="d-none d-lg-block">
  124. <TableOfContents />
  125. </div>
  126. </div>
  127. </div>
  128. <div className="flex-grow-1 flex-basis-0 mw-0">
  129. <Page />
  130. </div>
  131. </div>
  132. </>
  133. )}
  134. </div>
  135. </div>
  136. </div>
  137. </div>
  138. </ShareLinkLayout>
  139. </>
  140. );
  141. };
  142. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  143. const req: CrowiRequest = context.req as CrowiRequest;
  144. const { crowi } = req;
  145. const { configManager, searchService, xssService } = crowi;
  146. props.disableLinkSharing = configManager.getConfig('crowi', 'security:disableLinkSharing');
  147. props.isSearchServiceConfigured = searchService.isConfigured;
  148. props.isSearchServiceReachable = searchService.isReachable;
  149. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  150. props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  151. props.rendererConfig = {
  152. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  153. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  154. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  155. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  156. plantumlUri: process.env.PLANTUML_URI ?? null,
  157. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  158. // XSS Options
  159. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  160. attrWhiteList: xssService.getAttrWhiteList(),
  161. tagWhiteList: xssService.getTagWhiteList(),
  162. highlightJsStyleBorder: configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  163. };
  164. }
  165. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  166. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  167. props._nextI18Next = nextI18NextConfig._nextI18Next;
  168. }
  169. function getAction(props: Props): SupportedActionType {
  170. let action: SupportedActionType;
  171. if (props.isExpired) {
  172. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  173. }
  174. else if (props.shareLink == null) {
  175. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  176. }
  177. else {
  178. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  179. }
  180. return action;
  181. }
  182. async function addActivity(context: GetServerSidePropsContext, action: SupportedActionType): Promise<void> {
  183. const req: CrowiRequest = context.req as CrowiRequest;
  184. const parameters = {
  185. ip: req.ip,
  186. endpoint: req.originalUrl,
  187. action,
  188. user: req.user?._id,
  189. snapshot: {
  190. username: req.user?.username,
  191. },
  192. };
  193. await req.crowi.activityService.createActivity(parameters);
  194. }
  195. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  196. const req = context.req as CrowiRequest<IUserHasId & any>;
  197. const { crowi, params } = req;
  198. const result = await getServerSideCommonProps(context);
  199. if (!('props' in result)) {
  200. throw new Error('invalid getSSP result');
  201. }
  202. const props: Props = result.props as Props;
  203. try {
  204. const ShareLinkModel = crowi.model('ShareLink');
  205. const shareLink = await ShareLinkModel.findOne({ _id: params.linkId }).populate('relatedPage');
  206. if (shareLink != null) {
  207. props.isExpired = shareLink.isExpired();
  208. props.shareLink = shareLink.toObject();
  209. }
  210. }
  211. catch (err) {
  212. logger.error(err);
  213. }
  214. injectServerConfigurations(context, props);
  215. await injectNextI18NextConfigurations(context, props);
  216. await addActivity(context, getAction(props));
  217. return {
  218. props,
  219. };
  220. };
  221. export default SharedPage;